//
//  BleManager.h
//  leo
//
//  Created by Duke on 15/12/9.
//  Copyright © 2015年 mdx. All rights reserved.
//

#ifndef BleManager_h
#define BleManager_h


#endif /* BleManager_h */

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
// check device interval
#define CHECK_INTERVAL 8000
// The max tolerance of missing connection delayed
#define MAX_TOLERANCE_REPORT_DELAYED 10000

/*

 You can registerConnectionListener, and scanning will begin after register.
 The onDeviceConnected means device detected, you can begin listening state
 change, and begin to notify user. The onDeviceDisconnected will called when
 device become undetected. If you won't need listen state any more, call
 unRegisterListener.
 
 BE CARE: registerConnectionListener and unregsterConnectionListener is a
 pair, if you not call unregsterConnectionListener, the memory leak must
 occur.
 
 
 The processing of BleManager
 
    register listener
            |
            |
    dispatch scanDevices        [loop 1]
            |
            |
 found device from delegate     [loop 2]
            |
            |
    compare macaddress from     [loop 2]
    listener list
            |
            |
     if matched, set current
  time to listener data struct  [loop 2]
   to record last update time
            |
            |
        stop scan               [loop 3]
            |
            |
    check last update time
    is invalid, if not valid    [loop 3]
     notify user if invalid
            |
            |
    sceduleNextScan             [loop 3]
            |
            |
 mListenerList is not Empty     [loop 4]
            |
            |
    dispatch scanDevice
 
 */

@interface ConnectionListener : NSObject

-(void) onDeviceConnected : (NSObject*) cookie macAddress : (NSString*) macAddress;
-(void) onDeviceDisconnected : (NSObject*) cookie macAddress : (NSString*) macAddress;

@end

@interface BleManager : NSObject<CBCentralManagerDelegate, CBPeripheralDelegate>

+(BleManager*) getInstance;
+(BleManager*) getInstance:(BOOL)parameter;
-(void) registerConnectionListener : (ConnectionListener*) listener macAddress : (NSString*) macAddress userID: (NSString*)userID withCookie : (NSObject*) cookie;
-(void) unregisterConnectionListener : (NSString*) listener;

@end